package main

// 根据游戏表生成游戏名

import (
	"fmt"
	"log"
	"regexp"
	"server/base"
	"strconv"
	"strings"

	"github.com/360EntSecGroup-Skylar/excelize/v2"
)

type NameExporter struct {
}

func (self *NameExporter) stringPatchType(t int) string {
	switch t {
	case 1:
		return "降级"
	case 2:
		return "升级"
	case 3:
		return "金手指"
	case 4:
		return "中文"
	case 5:
		return "强制"
	}
	return "?"
}

func (self *NameExporter) formatM(m string) string {
	var out string
	if m == "" {
		return ""
	}
	l := strings.Split(m, "&")
	for _, s := range l {
		if strings.Contains(s, "[") {
			reg := regexp.MustCompile(`^(\d+)\[([\w.]+)\]$`)
			p := reg.FindStringSubmatch(s)
			if len(p) != 3 {
				log.Println("解析M失败", s)
				continue
			}
			t, err := strconv.Atoi(p[1])
			if err != nil {
				log.Println("解析M失败2", p[1], err)
				continue
			}
			ts := self.stringPatchType(t)
			v := p[2]
			item := fmt.Sprintf("[%s%s]", ts, v)
			out += item
		} else {
			t, err := strconv.Atoi(s)
			if err != nil {
				log.Println("解析M失败3", s, err)
				continue
			}
			ts := self.stringPatchType(t)
			item := fmt.Sprintf("[%s]", ts)
			out += item
		}
	}
	return out
}

// 过滤subid前缀, 返回(id, subid)
func (self *NameExporter) formatB(b string) (string, string) {
	for i, c := range b {
		if c >= 'A' && c <= 'Z' {
			return b[:i], b[i:]
		}
	}
	return b, ""
}

func (self *NameExporter) formatT(t string) string {
	switch t {
	case "HONG KONG":
		return "港版"
	case "EU":
		return "欧版"
	case "JAPAN":
		return "日版"
	case "US":
		return "美版"
	case "KR":
		return "韩版"
	}
	return ""
}

func (self *NameExporter) formatJ(j string) string {
	switch j {
	case "CN":
		return "中文"
	case "EN":
		return "英文"
	case "JP":
		return "日文"
	case "ETC":
		return "其他"
	}
	return ""
}

func (self *NameExporter) formatX(x string) string {
	switch x {
	case "Game":
		return "本体"
	case "Patch":
		return "补丁"
	case "Addon":
		return "DLC"
	}
	return "X"
}

func (self *NameExporter) formatG(g string) string {
	f, _ := strconv.ParseFloat(g, 64)
	return fmt.Sprintf("%.02f", f)
}

func (self *NameExporter) formatK(k string) string {
	f, _ := strconv.ParseFloat(k, 64)
	fs := strconv.FormatFloat(f, 'f', -1, 64)
	return fs
}

func (self *NameExporter) ReadExcel(filename string) []string {
	var out []string
	f, err := excelize.OpenFile(filename)
	if err != nil {
		log.Println(err)
		return out
	}
	sheets := f.GetSheetList()
	for _, shname := range sheets {
		rows, err := f.GetRows(shname)
		base.Raise(err)
		if len(rows) <= 0 {
			continue
		}
		for _, row := range rows[1:] {
			//fmt.Println("row:", row)
			var name string
			if len(row) <= 14 {
				continue
			}
			a := strings.TrimSpace(row[0]) // id

			b := strings.TrimSpace(row[1]) // subid
			if b != "" {
				id, bs := self.formatB(b)
				if a == "" {
					a = id
				}
				b = bs
			}
			if a == "" {
				//log.Printf("err: 没有id")
				continue
			}
			name += a
			if b != "" {
				name += "-" + b
			}
			d := strings.TrimSpace(row[3]) // 中文名
			name += "-" + d
			//alt := row[4]     // 别名
			//en := row[5]      // 英文名
			g := strings.TrimSpace(row[6]) // 版本号
			//genre := row[7]   // 游戏类型
			//players := row[8] // 游戏人数
			j := strings.TrimSpace(row[9])  // 语言 CN/EN/JP/ETC
			k := strings.TrimSpace(row[10]) // 安装前大小
			//dlc := row[11]    // DLC类型 1:DLC 2:主题
			m := strings.TrimSpace(row[12]) // PATCH类型 1:降级 2:升级 3:金手指 4:中文 5:强制
			//game := row[13]   // 游戏类型 1:PS4 2:PS2 3:模拟器
			r := strings.TrimSpace(row[17]) // 编号 CUSA07743
			t := strings.TrimSpace(row[19]) // 语言 HONG KONG/EU/JAPAN/US
			u := strings.TrimSpace(row[20]) // 最低版本 1.7,NA
			x := row[23]                    // Game:本体 Patch:补丁 Addon:dlc
			name += "-" + fmt.Sprintf("[%s]", r)
			if t != "" {
				name += fmt.Sprintf("[%s]", self.formatT(t))
			}
			if j != "" {
				name += fmt.Sprintf("[%s]", self.formatJ(j))
			}
			if x == "Game" {
				name += fmt.Sprintf("[%s]", self.formatX(x))
				name += fmt.Sprintf("[%s]", u)
			} else if x == "Patch" && m != "" {
				name += fmt.Sprintf("[%s%s]", self.formatM(m), self.formatX(x))
			} else {
				name += fmt.Sprintf("[%s]", self.formatX(x))
			}
			if g != "" {
				name += fmt.Sprintf("[v%s]", self.formatG(g))
			}
			name += fmt.Sprintf("[%sG]", self.formatK(k))
			fmt.Println("name:", name)
			out = append(out, name)
		}
	}
	return out
}
